<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>分配角色</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	  <link rel="stylesheet" href="/css/bootstrap.min.css" />
	   <link rel="stylesheet" href="/css/metroStyle.css" type="text/css">
	<!-- <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all" /> -->
</head>
<body class="childrenBody">
	<form class="" id="boxRoleForm">
        <ul id="treeDemo" class="ztree"></ul>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit="" lay-filter="editResources">立即提交</button>
				<!-- <button type="reset" class="layui-btn layui-btn-primary">重置</button> -->
		    </div>
		</div> 
	</form>
	<script type="text/javascript" src="/js/jquery-1.11.2.min.js"></script>
	<script type="text/javascript" src="/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="/js/jquery.ztree.excheck.js"></script>
	<script type="text/javascript" src="/plugins/layui/layui.js"></script>
<!-- 	<script type="text/javascript" src="/js/user/newsAdd.js"></script> -->
	
	<script type="text/javascript">
	layui.config({
		 base: '/js/admin/'
	}).extend({
	    ajaxExtention: 'ajaxExtention',//加载自定义扩展，每个业务js都需要加载这个ajax扩展
	    $tool: 'tool'
	}).use(['form','layer','jquery','ajaxExtention','$tool'],function(){
		var form = layui.form,
			layer = parent.layer === undefined ? layui.layer : parent.layer,
			$ = layui.jquery;
		 $tool = layui.$tool;
		 form.on("submit(editResources)",function(data){
	 		  var queryArgs = $tool.getQueryParam();//获取查询参数
	 	        var id = queryArgs['id'];
	 	       saveRoleResources(id);
	 	})
	     initUserInfo();
	     /**
	      * 初始化用户信息
	      * */
	      function initUserInfo() {
	         var queryArgs = $tool.getQueryParam();//获取查询参数
	         var rid = queryArgs['id'];
	         roleId = rid;
		        var setting = {
		            check: {
		                enable: true,
		                chkboxType:  { "Y" : "ps", "N" : "s" }
		            },
		            data: {
		                simpleData: {
		                    enable: true,
		                    idKey: "id",
		                    pIdKey: "parentid",
		                }
		            }
		        };

		        $.ajax({
		            async:false,
		            type : "POST",
		            data:{rid:rid},
		            url: "/resources/resourcesWithSelected",
		            dataType:'json',
		            success: function(data){

		            	
		                 $.fn.zTree.init($("#treeDemo"), setting, data.data);
		                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		                zTree.expandAll(true);
		                
		                form.render(); 
		               // $('#selectResources').modal();
		            }
		        });
	     }
	     
	      //保存权限的选择
	      function saveRoleResources(roleId) {
	          var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
	                  checkNode= zTree.getCheckedNodes(true);
	          var ids = new Array();
	          for(var i=0;i<checkNode.length;i++){
	              ids.push(checkNode[i].id);
	          }
	          $.ajax({
	              async:false,
	              type : "POST",
	              data:{roleid:roleId,resourcesid:ids.join(",")},
	            
	              url: "/roles/saveRoleResources",
	              success: function(data){
	                  if(data.code==2000){
	                	  layer.msg("角色权限更新成功！", {time: 1000}, function () {
	 	                         layer.closeAll("iframe");
	 	                         //刷新父页面
	 	                         parent.location.reload();
	 	                     });
	                  }
	              }
	          });
	      }
	})
	</script>
</body>
</html>

